# encoding: utf-8
"""
一些输出model组件
"""
import uuid
from typing import Literal
from pydantic import BaseModel, Field, model_validator


class Action(BaseModel):
    flowResult: list[dict] = Field(default_factory=list, description='action流程结果')
    id: str = Field(description='执行动作的ID')
    name: str = Field(description='动作的名称')
    plan_id: str = Field(description='父级任务的ID')
    sort_index: int = Field(description='执行的顺序')
    status: Literal["pending", "running", "failed", "completed"] = Field(default='pending', description='计划的状态')
    start_time: str = Field(default=None, description='任务开始时间')
    end_time: str | None = Field(default=None, description='任务结束时间')
    error: str | None = Field(default=None, description='错误信息')

    @model_validator(mode="before")
    @classmethod
    def set_default_id(cls, data):
        if "id" not in data or data["id"] is None:
            sort_index = data.get("sort_index")
            if sort_index is not None:
                data["id"] = f"action_{sort_index}"
            else:
                data["id"] = f"action_{uuid.uuid4()}"
        return data


class Plan(BaseModel):
    id: str = Field(description='计划的ID')
    sort_index: int = Field(description=''),
    name: str = Field(description='计划的名称')
    status: Literal["pending", "running", "failed", "completed"] = Field(default='pending', description='计划的状态')
    actions: list[Action] = Field(default=[], description='任务运行包含的所有动作')
    start_time: str = Field(default=None, description='任务开始时间')
    end_time: str | None = Field(default=None, description='任务结束时间')
    error: str | None = Field(default=None, description='错误信息')

    @model_validator(mode="before")
    @classmethod
    def set_default_id(cls, data):
        if "id" not in data or data["id"] is None:
            sort_index = data.get("sort_index")
            if sort_index is not None:
                data["id"] = f"plan_{sort_index}"
            else:
                data["id"] = f"plan_{uuid.uuid4()}"
        return data


class PlanSchema(BaseModel):
    plans: list[Plan] | list = Field(description='整体的计划列表')